import {Injectable} from '@angular/core';
import {HttpResponse} from '@angular/common/http';
import {ActivatedRouteSnapshot, Resolve, Router, Routes} from '@angular/router';
import {EMPTY, Observable, of} from 'rxjs';
import {flatMap} from 'rxjs/operators';

import {Authority} from 'app/shared/constants/authority.constants';
import {UserRouteAccessService} from 'app/core/auth/user-route-access-service';
import {ILabel, Label} from 'app/shared/model/label.model';
import {LabelService} from './label.service';
import {LabelComponent} from './label.component';
import {LabelDetailComponent} from './label-detail.component';
import {LabelUpdateComponent} from './label-update.component';

@Injectable({providedIn: 'root'})
export class LabelResolve implements Resolve<ILabel> {
  constructor(private service: LabelService, private router: Router) {
  }

  resolve(route: ActivatedRouteSnapshot): Observable<ILabel> | Observable<never> {
    const id = route.params['id'];
    if (id) {
      return this.service.find(id).pipe(
          flatMap((label: HttpResponse<Label>) => {
            if (label.body) {
              return of(label.body);
            } else {
              this.router.navigate(['404']);
              return EMPTY;
            }
          })
      );
    }
    return of(new Label());
  }
}

export const labelRoute: Routes = [
  {
    path: '',
    component: LabelComponent,
    data: {
      authorities: [Authority.USER],
      pageTitle: 'jhipsterSampleApplicationApp.label.home.title'
    },
    canActivate: [UserRouteAccessService]
  },
  {
    path: ':id/view',
    component: LabelDetailComponent,
    resolve: {
      label: LabelResolve
    },
    data: {
      authorities: [Authority.USER],
      pageTitle: 'jhipsterSampleApplicationApp.label.home.title'
    },
    canActivate: [UserRouteAccessService]
  },
  {
    path: 'new',
    component: LabelUpdateComponent,
    resolve: {
      label: LabelResolve
    },
    data: {
      authorities: [Authority.USER],
      pageTitle: 'jhipsterSampleApplicationApp.label.home.title'
    },
    canActivate: [UserRouteAccessService]
  },
  {
    path: ':id/edit',
    component: LabelUpdateComponent,
    resolve: {
      label: LabelResolve
    },
    data: {
      authorities: [Authority.USER],
      pageTitle: 'jhipsterSampleApplicationApp.label.home.title'
    },
    canActivate: [UserRouteAccessService]
  }
];
